<template>
  <el-dialog title="选择地铁站" :visible.sync="visible" width="900px" top="5vh" append-to-body>
    <el-card class="filter-container" shadow="never">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true">
        <el-form-item label="地铁站名称" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入地铁站名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-row>
      <div class="table-container">
        <el-table @row-click="clickRow" ref="table" :data="tableList" @selection-change="handleSelectionChange" height="260px" border
                  :default-expand-all="isExpandAll" row-key="subwayId"
                  :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column prop="subwayId" label="ID" :show-overflow-tooltip="true"></el-table-column>
          <el-table-column prop="name" label="线路/站点" :show-overflow-tooltip="true"></el-table-column>
          <el-table-column prop="city.cityName" label="所属城市" :show-overflow-tooltip="true"></el-table-column>
        </el-table>
      </div>


    </el-row>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="confirm">确 定</el-button>
      <el-button @click="visible = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { listSubway } from "@/api/basic/subway";
  import Treeselect from "@riophae/vue-treeselect";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";

  export default {
    components: {
      Treeselect,
    },
    name: "SelectSubway",
    data() {
      return {
        // 遮罩层
        visible: false,
        // 总条数
        total: 0,
        // 表数据
        tableList: [],
        // 是否展开，默认全部不展开
        isExpandAll: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          name: undefined,
          // 日期范围
          dateRange: [],
        },
        // 选中数组
        checkList: [],
        checkData: {}
      }
    },
    methods: {
      // 显示弹框
      show() {
        this.getList();
        this.visible = true;
      },
      clickRow(row) {
        this.$refs.table.toggleRowSelection(row);
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.checkList = selection;
        this.checkData.names = selection.map(item => item.name).join(",");
        this.checkData.ids = selection.map(item => item.subwayId).join(",");
      },
      // 查询表数据
      getList() {
        listSubway(this.queryParams).then(res => {
          if (res.code === 200) {
            this.tableList = this.handleTree(res.data, "subwayId");
          }
        });
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      handleSizeChange(val) {
        this.queryParams.pageNum = 1
        this.queryParams.pageSize = val
        this.getList()
      },
      handleCurrentChange(val) {
        this.queryParams.pageNum = val
        this.getList()
      },
      /** 确认选择 */
      confirm() {
        this.$emit("confirmSubway", this.checkData);
        this.visible = false;
      },
    }
  }
</script>
